const http = require('./http');

const TOPICS_BASE_URL = '/commonTopics/'
// 根据单元id获取知识点列表
const TOPICS_BY_UNITID = '/commonTopics/byUnitId/';

const topicAPI = {
  /**
   * 根据单元id获取知识点列表
   * @param {String} unitId
   */
  getTopicsByUnitID(unitId, data, callback) {
    const url = TOPICS_BY_UNITID + unitId;
    http.get(url, data, callback)
  },
  /**
   * 根据单元id、课文id和知识点id获取知识点详情
   *
   * @param {String} topicId
   * @param {Object} data { unitId: String, lessonId: String }
   * @param {Function} callback
   */
  getTopicDetailByID(topicId, data, callback) {
    const url = TOPICS_BASE_URL + topicId;
    http.get(url, data, callback)
  },
  /**
   * 收藏知识点
   *
   * @param {String} topicId
   */
  postCollectTopic(topicId, callback) {
    const url = TOPICS_BASE_URL + topicId + '/favorites';
    http.post(url, null, callback)
  },
  /**
   * 取消收藏知识点
   *
   * @param {String} topicId
   */
  deleteCollectTopic(topicId, callback) {
    const url = TOPICS_BASE_URL + topicId + '/favorites';
    http.delete(url, null, callback)
  },
  searchTopics(data, callback) {
    let url = '/commonTopics';
    http.get(url, data, callback)
  },
  getTopics(data, callback) {
    let url = '';
    let params = {};
    if (data.unitId) {
      url = TOPICS_BY_UNITID + data.unitId;
      data.pageNo && (params.pageNo = data.pageNo)
      data.pageSize && (params.pageSize = data.pageSize)
    } else {
      url = '/commonTopics';
      params = data;
    }
    console.log(url);
    http.get(url, params, callback)
  },
  getCollectedTopics(data, callback) {
    let url = '/commonTopics/favorites';
    http.get(url, data, callback)
  },
  getAssocitatedBooks(topicId, callback) {
    let url = '/commonTopics/' + topicId +  '/textbooks';
    http.get(url, null, callback)
  }
}

module.exports = topicAPI